import {
	uploadUrl
} from '@/pages/api/report/helperOrderp.js'
import {
	addwgxnd,
	addwgczxx,
	addwgaj
} from '@/pages/api/report/orderp.js'
// import { getaddEvent } from '@/api/wisdomEmergency/wisdom.js'
// import { getUrbanType } from '@/api/orderp/cityManagement/api.js'
import {
	mapState
} from 'vuex'
export default {
	computed:{
		...mapState(['userInfo'])
	},
	methods: {
		//返回上一级
		goBack() {
			uni.navigateBack()
		},
		//获取当前位置
		getLocation() {
			uni.getLocation({
				type: 'wgs84',
				geocode: true,
				success: res => {
					console.log(res, 888)
					this.form.lat = res.latitude
					this.form.lng = res.longitude
					this.getaddress(this.form.lng,this.form.lat)
					// this.form.address = res.latitude + ',' + res.longitude
					// if(res.address){
					// 	this.form.address = res.address.province + res.address.city + res.address.district + res.address.street + res.address.streetNum + res.address.poiName
					// 	// this.addressModalshow = false
					// 	uni.hideLoading();
					// } else {
					// 	console.log(5555)
					// 	this.getLocation()
					// }
				}
			});
		},

		//显示是否有效选择器
		showValidityTerm() {

			this.ValidityTermSelectShow = true

		},
		//选择类型后
		streetSelectConfirm(val) {
			this.form.street = val[0].label
		},
		//显示是否有效选择器
		showstreet() {

			this.streetSelectShow = true

		},
		//选择类型后
		ValidityTermSelectConfirm(val) {
			this.form.fieldValidityTerm = val[0].label
		},
		//文件tab点击  0录音 1拍照 2相册  照片限定最多2张
		fileTabChoose(index) {
			let photoCount = 10 - this.photoList.length
			if (index === 0) {
				if (photoCount != 0) {
					uni.chooseImage({
						sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
						sourceType: ['camera'], //从相册选择
						success: res => {
							console.log(res)
							res.tempFiles.forEach(e => {
								this.requestUploadImg(e.path).then(res => {
									if (res) {
										this.photoList.push(res)
									}
								})
							})
						}
					});
				}
			} else if (index === 1) {
				if (photoCount != 0) {
					uni.chooseImage({
						count: photoCount, //默认9
						sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
						sourceType: ['album'], //从相册选择
						success: res => {
							console.log(res)
							res.tempFiles.forEach(e => {
								this.requestUploadImg(e).then(res => {
									if (res) {
										this.photoList.push(res)
									}
								})
							})
						}
					});
				}
			}
		},
		//删除照片
		delPhoto(index) {
			this.photoList.splice(index, 1)
		},
		//上传照片
		requestUploadImg(filePath) {
			console.log(filePath)
			return new Promise((resolve, reject) => {
				uni.uploadFile({
					url: uploadUrl,
					formData: {
						'file': filePath
					},
					success: (uploadFileRes) => {
						console.log(uploadFileRes)
						if (uploadFileRes.statusCode == 200) {
							resolve(JSON.parse(uploadFileRes.data).data)
						} else {
							reject('上传失败')
						}
					},
					fail: err => {
						reject(err)
					}
				});
			})
		},
		
		//点击新增按钮
		doAdd() {
						this.$refs.uForm.validate((valid,a,b) => {
							console.log(valid,a,b)
							if (valid) {
								console.log('验证通过');
							} else {
								console.log('验证失败');
							}
						});
				
			// this.$refs.uForm.validate(valid => {
			
			// 	if (valid) {
			// 		var that = this
			// 		uni.showModal({
			// 		    title: '提示',
			// 		    content: '是否确定上报？',
			// 			 confirmText: '确定',
			// 			  cancelText: '取消',
			// 		    success: res=> {
			// 		        if (res.confirm) {
			// 		            this.addSure()
			// 		        } else if (res.cancel) {
							
			// 		            console.log('用户点击取消',this,that);
			// 		        }
			// 		    }
			// 		});
					
			
			// 	} else {
			// 		console.log("验证失败")
			// 	}
			// })
		},
		// 确认新增
		addSure() {
			console.log(this.photoList)
			console.log(this.voiceSuccessForm, 666)
			let params = {
				address: this.form.address,
				fieldValidityTerm: this.form.fieldValidityTerm,
				enclosurePath:this.form.enclosurePath,
				informationNote: this.form.informationNote,
				isValid: this.form.isValid,
				lat: this.form.lat,
				lon: this.form.lng,
				name: this.form.name,
				reportUserId: this.form.reportUserId,
				reportUserName: this.form.reportUserName,
				street: this.form.street,
				showclosingTime: this.form.showclosingTime,
				showfilingTime:this.form.showfilingTime,
				arrestTimeTime:this.form.arrestTimeTime,
				departureTimeTime:this.form.departureTimeTime,
			}
			console.log('参数', params)
			if(this.reporttype==0){
				addwgxnd(params).then(res => {
					this.addModalshow = false
					console.log('ssss' + res)
				
						this.$refs.uToast.show({
							title: '新增上报成功',
							type: 'success'
						})
						this.form = {
								address: '',
								name: '',
								street: '',
								enclosurePath:'',
								reportUserName: '',
								fieldValidityTerm: '',
								informationNote: '',
								isValid: 0,
								reportUserId: '',
								lat: '',
								lng: '',
							},
							setTimeout(() => {
								uni.navigateBack()
							}, 500)
				
				}).catch(err => {
					console.log('s上报错误', err)
				})
			}
			else if(this.reporttype==1){
				addwgczxx(params).then(res => {
					this.addModalshow = false
					console.log('ssss' + res)
				
						this.$refs.uToast.show({
							title: '新增上报成功',
							type: 'success'
						})
						this.form = {
								address: '',
								name: '',
								street: '',
								enclosurePath:'',
								reportUserName: '',
								fieldValidityTerm: '',
								informationNote: '',
								isValid: 0,
								reportUserId: '',
								lat: '',
								lng: '',
							},
							setTimeout(() => {
								uni.navigateBack()
							}, 500)
							})
			}
			else if(this.reporttype==2){
				addwgaj(params).then(res => {
					this.addModalshow = false
					console.log('ssss' + res)
				
						this.$refs.uToast.show({
							title: '新增上报成功',
							type: 'success'
						})
						this.form = {
								address: '',
								name: '',
								street: '',
								enclosurePath:'',
								reportUserName: '',
								fieldValidityTerm: '',
								informationNote: '',
								isValid: 0,
								reportUserId: '',
								lat: '',
								lng: '',
							},
							setTimeout(() => {
								uni.navigateBack()
							}, 500)
							})
			}
			
		},
		//重新定位
		rePostioning() {
			// this.addressModalshow = true
			uni.showLoading({
				title: '正在获取当前位置'
			});
			setTimeout(() => {
				this.getLocation()
			}, 800)
		},
		//上传成功回调
		upsuccess(res, b, c) {
			console.log(res,b,c)
			if (res.code == 200) {
				this.photoList.push(res.data.fileUrl)
			}
			this.form.enclosurePath = this.photoList.join(',')
		},
		uperr(a, b, c) {
			console.log(a, b, c)
		},
		beforeUpload(){
			console.log(a,b,c)
		},
		getaddress(lat,lon){
			this.test.getRegeo({
				location: lat + ',' + lon, //location的格式为'经度,纬度'
				//成功回调
				success: (data)=> {
					console.log(data[0].regeocodeData.formatted_address,data[0].name)
					this.form.address = data[0].regeocodeData.formatted_address
				},
				fail: function(info) {
					//失败回调
					console.log(info)
				}
			})
		}

	}
}
